package com.zhou.jianzhi.controller.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhou.jianzhi.common.AjaxResult;
import com.zhou.jianzhi.entity.dto.ResumeDTO;
import com.zhou.jianzhi.entity.po.Resume;
import com.zhou.jianzhi.service.IResumeService;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;


/**
* ResumeController
*
* @author zhou
* @date 2021-01-03
*/
@RestController
@RequestMapping("/resume")
public class ResumeController {
    @Autowired
    private IResumeService resumeService;


    /**
     * 根据${table.name}表id查询${table.name}
     */
    @GetMapping("/getById")
    public AjaxResult selectById(@RequestParam Integer id) throws Exception{
        Resume resume = resumeService.selectById(id);
        if (resume==null){
            return AjaxResult.error("查不到");
        }
        return AjaxResult.success();
    }

    /**
     * 上传简历
     */
    @PostMapping("/upload")
    public AjaxResult uploadResume(@RequestParam("file") MultipartFile file,
                                   @RequestParam("jobSeekerId") Integer jobSeekerId) {
        try {
            // 获取文件名
            String fileName = file.getOriginalFilename();
            // 获取文件类型
            String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
            // 获取文件内容
            byte[] fileContent = file.getBytes();

            Resume resume = new Resume();
            resume.setJobSeekerId(jobSeekerId);
            resume.setResFileName(fileName);
            resume.setResFileType(fileType);
            resume.setResFile(fileContent);
            resume.setCreateTime(new Date());

            resumeService.save(resume);

            return AjaxResult.success("上传成功");
        } catch (Exception e) {
            return AjaxResult.error("上传失败：" + e.getMessage());
        }
    }
    @GetMapping("/download/{jobSeekerId}")
    public void downloadResume(@PathVariable Integer jobSeekerId, HttpServletResponse response) {
        try {
            QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("job_seeker_id", jobSeekerId);
            Resume resume = resumeService.getOne(queryWrapper);
            if (resume != null && resume.getResFile() != null) {
                response.setContentType("application/octet-stream");
                response.setHeader("Content-Disposition", "attachment; filename=" +
                        URLEncoder.encode(resume.getResFileName(), "UTF-8"));

                // 使用 OutputStream 直接写入
                ServletOutputStream outputStream = response.getOutputStream();
                outputStream.write(resume.getResFile());
                outputStream.flush();
                outputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/getOne")
    public AjaxResult selectOneResume(@RequestBody ResumeDTO resumeDTO) throws Exception{
        Resume resume = resumeService.selectOneResume(resumeDTO);
        if (resume==null){
            return AjaxResult.error("查不到");
        }
        return AjaxResult.success(resume);
    }


    /**
     * 查询resume列表
     */
    @PostMapping("/list")
    public AjaxResult list(@RequestBody ResumeDTO resumeDTO) throws Exception{
        return AjaxResult.success(resumeService.selectResumeList(resumeDTO));
    }


    /**
     * 新增单个resume
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ResumeDTO resumeDTO) throws Exception{
        return AjaxResult.success(resumeService.addResume(resumeDTO));
    }


    /**
     * 批量新增resume
     */
    @PostMapping("/addList")
    public AjaxResult addList(@RequestBody List<ResumeDTO> resumeDTOList) throws Exception{
        return AjaxResult.success(resumeService.insertResumeList(resumeDTOList));
    }

    /**
     * 根据id修改resume
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ResumeDTO resumeDTO) {
        return AjaxResult.success(resumeService.updateResume(resumeDTO));
    }

    /**
     * 根据id删除resume
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable int id) {
        return AjaxResult.success(resumeService.deleteResumeById(id));
    }


}
